package com.derbysoft.synxisadapter.action.settings.channel;

import com.derbysoft.core.LimitedList;
import com.derbysoft.synxisadapter.action.DMXAbstractAction;
import com.derbysoft.synxisadapter.commons.exception.ChannelExistedException;
import com.derbysoft.synxisadapter.commons.exception.ChannelNotFoundException;
import com.derbysoft.synxisadapter.commons.model.Channel;
import com.derbysoft.synxisadapter.commons.repository.ChannelRepository;
import com.derbysoft.synxisadapter.commons.repository.impl.ChannelCondition;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * User: jason
 * Date: 2013-02-21
 */
public class DMXAction extends DMXAbstractAction {

    @Autowired
    private ChannelRepository channelRepository;
    private ChannelCondition condition = new ChannelCondition();

    @Action("dmxQueryChannels")
    public String dmxQueryChannels() {
        try {
            LimitedList<Channel> limitedList = channelRepository.findByCondition(condition, createLimit());
            return populateSucceededJsonResult(dmxPaging(limitedList, super.getPage(), limitedList.getTotalcount()));
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxLoadChannel")
    public String dmxLoadChannel() {
        try {
            if (condition == null || StringUtils.isBlank(condition.getPassport())) {
                return populateSucceededJsonResult("channel passport must be not null !");
            }
            Channel existed = channelRepository.findByPassport(condition.getPassport());
            if (existed == null) {
                throw new ChannelNotFoundException(condition.getPassport());
            }
            return populateSucceededJsonResult(existed);
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxUpdateChannel")
    public String dmxUpdateChannel() {
        try {
            if (condition == null) {
                return populateSucceededJsonResult("channel condition must be not null !");
            }
            Channel existed = channelRepository.findByPassport(condition.getPassport());
            if (existed == null) {
                throw new ChannelNotFoundException(condition.getPassport());
            }
            existed.setId(condition.getId());
            existed.setCode(condition.getCode());
            existed.setName(condition.getName());
            existed.setIataNumber(condition.getIataNumber());
            existed.setUserName(condition.getUserName());
            existed.setPassword(condition.getPassword());
            existed.setTaxPolicy(condition.getTaxPolicy());
            existed.setInclusiveTaxRate(StringUtils.equals(condition.getInclusiveTaxRate(), "Y"));
            channelRepository.save(existed);
            return populateSucceededJsonResult(existed);
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxSaveChannel")
    public String dmxSaveChannel() {
        try {
            if (condition == null) {
                return populateSucceededJsonResult("channel condition must be not null !");
            }
            Channel existed = channelRepository.findByPassport(condition.getPassport());

            if (existed != null) {
                throw new ChannelExistedException(condition.getPassport());
            }
            existed = new Channel();
            existed.setPassport(condition.getPassport());
            existed.setId(condition.getId());
            existed.setCode(condition.getCode());
            existed.setName(condition.getName());
            existed.setIataNumber(condition.getIataNumber());
            existed.setUserName(condition.getUserName());
            existed.setPassword(condition.getPassword());
            existed.setTaxPolicy(condition.getTaxPolicy());
            existed.setInclusiveTaxRate(StringUtils.equals(condition.getInclusiveTaxRate(), "Y"));
            channelRepository.save(existed);
            return populateSucceededJsonResult(existed);
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxDeleteChannel")
    public String dmxDeleteHotel() {
        try {
            if (condition == null || StringUtils.isBlank(condition.getPassport())) {
                return populateSucceededJsonResult("channel passport must be not null !");
            }
            Channel existed = channelRepository.findByPassport(condition.getPassport());
            if (existed == null) {
                throw new ChannelNotFoundException(condition.getPassport());
            }
            channelRepository.delete(existed);
            return populateSucceededJsonResult(String.format("delete done [%s].", condition.getPassport()));
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    public ChannelCondition getCondition() {
        return condition;
    }

    public void setCondition(ChannelCondition condition) {
        this.condition = condition;
    }
}
